<template>
  <div class="lable-top">
    <el-form label-position="top" size="mini">
      <el-row :gutter="10">
        <el-col v-permission="['super']" :span="4">
          <el-form-item label="组织">
            <el-select v-model="search.org_id" filterable>
              <el-option
                v-for="(item, index) in company"
                :key="index"
                :label="item.name"
                popper-class="w100"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-permission="['super']" :span="4">
          <el-form-item label=" ">
            <el-button @click="loadingCertificate">查询终端</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-form label-position="top" size="small">
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="服务端公钥证书">
            <el-input
              v-model="cert.server_public_key"
              :autosize="{ minRows: 25, maxRows: 100 }"
              readonly
              type="textarea"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="客户端公钥证书">
            <el-input
              v-model="cert.client_public_key"
              :autosize="{ minRows: 25, maxRows: 100 }"
              readonly
              type="textarea"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="客户端私钥证书">
            <el-input
              v-model="cert.client_private_key"
              :autosize="{ minRows: 25, maxRows: 100 }"
              readonly
              type="textarea"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="el-row--flex is-justify-center">
      <el-button style="width: 50%" @click="reset">
        重置证书
      </el-button>
    </div>
  </div>
</template>

<script>
import org_getall from "@/utils/mixin/org_getall";
import org from "@/api/org";
export default {
  name: "Certificate",
  mixins: [org_getall],
  data: () => {
    return {
      search: {
        org_id: ""
      },
      cert: {
        client_private_key: "",
        client_public_key: "",
        server_public_key: ""
      }
    };
  },
  mounted() {
    this.loadingData();
  },
  methods: {
    async loadingData() {
      await this.loadingOrgGetall();
      await this.loadingCertificate();
    },
    loadingCertificate() {
      return org.cert_get({ org_id: this.search.org_id }).then(res => {
        this.$set(this, "cert", res.data.cert);
      });
    },
    reset() {
      this.$confirm("您真的需要重置证书吗？请再次确认", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return org.org_resetkeypair({ org_id: this.search.org_id });
        })
        .then(res => {
          this.$message.success(res.data.message);
          this.loadingCertificate();
        });
    }
  }
};
</script>

<style scoped></style>
